package com.spring.vencent.factory;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

import com.spring.vencent.service.DBService;
import com.spring.vencent.service.impl.DBServiceImpl;

/**
 * 调用时，也就是lookup查找服务时, 服务实例化
 * @author 1d
 *
 */
public class DBServiceFactory implements ObjectFactory{

	/**
	 * 说白了， 就是创建一个服务对象， 
	 */
	@Override
	public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
			throws Exception {
		 if (obj instanceof Reference) { 
			 Reference ref = (Reference) obj;
			 //获取属性，然后给服务对象赋值
			 String location = (String) ref.get("location").getContent();
			 String state = (String) ref.get("state").getContent();
			 DBService db = new DBServiceImpl();
			 db.setProperty(0, location);
			 db.setProperty(1, state);
			 return db;
		 }
		return null;
	}

}
